<?php
/*
 * @Author: lokei
 * @Date: 2022-08-20 17:03:15
 * @LastEditors: lokei
 * @LastEditTime: 2022-09-19 13:32:27
 * @Description: 
 */

namespace App\Http\Controllers\Scrm\Clerk;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Scrm\Clerk\ClerkModel;
use App\Models\Scrm\Department\DepartmentModel;
use App\Models\Store\StoreModel;
use App\Models\Ucenter\UserModel;
use Illuminate\Http\Request;

class ClerkController extends Controller
{
    public function ofDepartment(Request $request)
    {
        $department = DepartmentModel::where('id', '=', $request->input('id'))->first();
        if($department->parentid == 0) {
            $clerk_list = ClerkModel::get();
        } else {
            $clerk_list = ClerkModel::where('department_id', '=', $request->input('id'))->get();
        }
        $res = ResultTool::success();
        $res['data']['items'] = $clerk_list;
        $res['data']['total'] = count($clerk_list);
        return $res;
    }

    public function get(Request $request)
	{
		$clerk = ClerkModel::where('id', '=', $request->input('id'))->first();
        $res = ResultTool::success();
		$res['data']['clerk'] = $clerk;
		return $res;
	}
	public function ofMe()
	{
		$user = app('auth')->user();
		$clerk = ClerkModel::where('uid', '=', $user->uid)->first();
        $res = ResultTool::success();
		$res['data']['clerk'] = $clerk;
		return $res;
	}
	public function list(Request $request)
	{
		if($request->has('ids')) {
			$ids = explode(',', ($request->input('ids')));
			$items = ClerkModel::whereIn('id', $ids)->get();
		} else {
			$items = ClerkModel::get();
		}
        $res = ResultTool::success();
		$res['data']['items'] = $items;
		$res['data']['count'] = count($items);
		return $res;
	}
	public function update(Request $request)
	{
		$user = UserModel::where('mobile', '=', $request->input('mobile'))->first();
		if (is_null($user)) {
            $res = ResultTool::fail();
            $res['errorMsg'] = '用户不存在';
            return $res;
		} else {
			$clerk = ClerkModel::where('id', '=', $request->input('id'))->first();
			$store = StoreModel::where('id', '=', $request->input('store_id'))->first();
			$clerk->store_id = $store->id;
			$clerk->store_name = $store->name;
			$clerk->uid = $user->uid;
			$clerk->name = $request->input('name');
			$clerk->mobile = $request->input('mobile');
			$clerk->save();
            return ResultTool::success();
		}
	}
	public function create(Request $request)
	{
		$user = UserModel::where('mobile', '=', $request->input('mobile'))->first();
		if (is_null($user)) {
			$res = ResultTool::fail();
			$res['errorMsg'] = '手机号尚未在手机端绑定用户！';
			return $res;
		} else {
			$store = StoreModel::where('id', '=', $request->input('store_id'))->first();
			$clerk = new ClerkModel();
			$clerk->store_id = $store->id;
			$clerk->store_name = $store->name;
			$clerk->uid = $user->uid;
			$clerk->name = $request->input('name');
			$clerk->mobile = $request->input('mobile');
			$clerk->createtime = date('Y-m-d H:i:s', time());
			$clerk->save();
			return ResultTool::success();
		}
	}
	public function delete(Request $request)
	{
		ClerkModel::where('id', '=', $request->input('id'))->delete();
		return ResultTool::success();
	}

}
